﻿using MySql.Data.MySqlClient;
using RantopadService.Conn;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace RantopadService.Business
{
    public class AdminBusiness
    {
        public static bool  DoLogin(string admin_name,string admin_psw)
        {
            string mysql = "SELECT * from admin where ADMIN_NAME=\"" + admin_name+"\"";
            //创建数据库连接
            var myconnection = MyMySqlHelper.GetConn();
            myconnection.Open();
            //创建MySqlCommand对象
            MySqlCommand mycommand = new MySqlCommand(mysql, myconnection);
            MySqlDataReader dr = mycommand.ExecuteReader();
            
            if (!dr.Read())
                return false;
            var drPwd = dr["ADMIN_PSW"].ToString();
            dr.Close();
            if(dr!=null)
            {
                if (drPwd == admin_psw)
                    return true;
            }
            myconnection.Close();
            return false;
        }
        public static void  IfLogined(System.Web.UI.Page page)
        {
            if (!page.IsPostBack)
            {
                if (page.Session["logid"] == null)
                    page.Response.Redirect("Login.aspx");
                else
                {
                    var logid = page.Session["logid"].ToString();
                    if (logid == null || logid == "")
                    {
                        page.Response.Redirect("Login.aspx");
                    }
                }

            }
            
        }
        public static DataTable getAllAdmins(string wherelike)
        {
            string mysql = "SELECT * from admin  where ADMIN_NAME like '%{0}%'";
            mysql = string.Format(mysql, wherelike);
            //创建数据库连接
            var myconnection = MyMySqlHelper.GetConn();
            myconnection.Open();
            var dataAdapter = new MySqlDataAdapter(mysql, myconnection);
            var dt = new DataTable();
            dataAdapter.Fill(dt);
            myconnection.Close();

            return dt;
        }
        public static int getAllAdminsCount()
        {
            string mysql = "SELECT count(*) as count from admin ";
            //创建数据库连接
            var myconnection = MyMySqlHelper.GetConn();
            myconnection.Open();
            MySqlCommand mycommand = new MySqlCommand(mysql, myconnection);
            MySqlDataReader dr = mycommand.ExecuteReader();

            if (!dr.Read())
                return 0;
            var countstr = dr["count"].ToString();
            var count = Convert.ToInt32(countstr);
            myconnection.Close();
            return count;
        }
        public static int getAllAdminsCount(string wherelike, string online)
        {
            string mysql = "SELECT count(ADMIN_NAME) as count from admin where ADMIN_NAME like '%{0}%' ";
            mysql = string.Format(mysql, wherelike);
            //创建数据库连接
            var myconnection = MyMySqlHelper.GetConn();
            myconnection.Open();
            MySqlCommand mycommand = new MySqlCommand(mysql, myconnection);
            MySqlDataReader dr = mycommand.ExecuteReader();

            if (!dr.Read())
                return 0;
            var countstr = dr["count"].ToString();
            var count = Convert.ToInt32(countstr);
            myconnection.Close();
            return count;
        }
        public static int checkAdminId(string adminID)
        {
            var count = checkData("ADMIN_NAME", adminID);
            return count;
        }
        public static int checkData(string colNM, string adminID)
        {
            string mysql = "SELECT count(ADMIN_NAME) as count from admin where {0} = '{1}' ";
            mysql = string.Format(mysql, colNM, adminID);
            //创建数据库连接
            var myconnection = MyMySqlHelper.GetConn();
            myconnection.Open();
            MySqlCommand mycommand = new MySqlCommand(mysql, myconnection);
            MySqlDataReader dr = mycommand.ExecuteReader();

            if (!dr.Read())
                return 0;
            var countstr = dr["count"].ToString();
            var count = Convert.ToInt32(countstr);
            myconnection.Close();
            return count;
        }

        
        public static int delAdminByName(string adminName)
        {
            string mysql = "delete from admin where ADMIN_NAME='{0}'";
            mysql = string.Format(mysql, adminName);
            //创建数据库连接
            var myconnection = MyMySqlHelper.GetConn();
            myconnection.Open();
            MySqlCommand mycommand = new MySqlCommand(mysql, myconnection);

            var count = mycommand.ExecuteNonQuery();

            myconnection.Close();
            return count;
        }



        public static int addAdmin( string name, string psd)
        {
            //INSERT INTO `rantopad`.`admin` (`ADMIN_NAME`, `ADMIN_PSW`, `EMAIL`, `ONLINE`) VALUES ('aaaaa', 'a', 'liang@163.com', '0');
            int count = 0;
            try
            {
                string mysql = "insert into `admin` (`ADMIN_NAME`, `ADMIN_PSW`) VALUES ('{0}', '{1}') ";
                mysql = string.Format(mysql, name, psd);
                //创建数据库连接
                var myconnection = MyMySqlHelper.GetConn();
                myconnection.Open();
                MySqlCommand mycommand = new MySqlCommand(mysql, myconnection);

                count = mycommand.ExecuteNonQuery();
                myconnection.Close();
            }
            catch (Exception)
            {

                count = 0;
            }


            return count;
        }

        internal static int updateAdmin( string name, string psd)
        {
            int count = 0;
            try
            {
                string mysql = "update `admin` set `ADMIN_PSW`='{1}'  where `ADMIN_NAME`='{0}'";
                mysql = string.Format(mysql, name, psd);
                //创建数据库连接
                var myconnection = MyMySqlHelper.GetConn();
                myconnection.Open();
                MySqlCommand mycommand = new MySqlCommand(mysql, myconnection);

                count = mycommand.ExecuteNonQuery();
                myconnection.Close();
            }
            catch (Exception)
            {

                count = 0;
            }


            return count;
        }
    }
}